#include "managerview_console_impl.h"
#include "manager.h"
#include <stdlib.h>
#include <iostream>
#include "tools.h"

using namespace std;
using namespace MAN;

ManagerViewConsoleImpl::ManagerViewConsoleImpl()
{
	service = new ManagerServiceImpl;
}

void ManagerViewConsoleImpl::menu(void)//显示主菜单 
{
	while(1)
	{
		int choice = 0;
		do
		{
			system("clear");
			puts("╭════════════════════════════════════════════════════╮");  
   			puts("│               　企业管理子系统　　　 　   　 　    │");  
		    puts("╰════■□■□══════════════════════════════════■□■□══════╯"); 
		    puts("");
			cout << "		  1、增加管理员" << endl;
			cout << "		  2、删除管理员" << endl;
			cout << "		  3、列出所有管理员" << endl;
			cout << "		  4、返回上一层" << endl;
			puts("");
			puts("╭════════════════════════════════════════════════════╮"); 
			puts("╰════■□■□══════════════════════════════════■□■□══════╯"); 
			puts("");
			cout << "		  请选择:";		
			cin >> choice ;
		}while(choice < 1 || choice >4);
		system("clear");
		switch(choice)
		{
			case 1: add();  break;
			case 2: del();	break;
			case 3: list(); break;
			case 4: return;
		}
		clear_stdin();
		anykey_continue();
	}
}

//处理增加管理员菜单项
void ManagerViewConsoleImpl::add(void)
{
	char buff1[100] = {};
	char buff2[100] = {};
	cout << "请输入用户名:" ;
	cin >> buff1;
	clear_stdin();
	
	cout << "请输入密码：" ;
	cin >> buff2;
	clear_stdin();
	int MgrID = generator_MgrId(MgrID_PATH);
	Manager m(buff1,buff2,MgrID);
	
	if(service->addManager(m))
	{
		cout << "添加管理员成功 ！" <<endl;
		cout << "管理员ID: " << MgrID << endl;
	}
	else
	{
		cout << "添加管理员失败 ！" <<endl;
	}
} 

//处理删除管理员菜单项 
void ManagerViewConsoleImpl::del(void)
{
	int temp_id;
	cout << "请输入管理员ID：" ;
	cin >> temp_id;
	clear_stdin();
	cout << "确认删除管理员 y/n ! ";
	
	if(getchar() == 'y')
	{
		if( service->deleteManager(temp_id) )
		{
			cout << "删除管理员成功！"<< endl;
		}
		else
		{
			cout << "管理员ID不存在！" << endl;
		}
	}
	else
	{
		cout << "删除管理员失败！" << endl;
	}
}

//处理列出所有管理员菜单项 
void ManagerViewConsoleImpl::list(void)
{
	vector<Manager> temp_vec = service -> listManager();
	clear_stdin();
	cout << "*********************************************" << endl;
	for(unsigned int i= 0; i < temp_vec.size();i++)
	{
		cout << temp_vec[i] << endl;
	}
	cout << "*********************************************" << endl;

}

ManagerViewConsoleImpl::~ManagerViewConsoleImpl()
{
	delete service;
}
	

